<!--
 * @name warnUp
 * @description 组件描述
 * @author idouya
 * @email elegentfrog@163.com
 * @date 2025-07-01
 -->

<template>
  <div class="p-warn-up common-bg common-layout">
    <page-title
      :discipline="gameInfo.discipline"
      :event="gameInfo.event"
      :match="gameInfo.match"
      :type="gameInfo.type"
      :startTime="gameInfo.startTime"
    ></page-title>
    <div class="conetnt">
      <div class="info">{{ gameInfo.order }}</div>
    </div>
  </div>
</template>

<script setup>
import useStore from '@/store'
import Storage from '@/assets/js/storage'
import Utils from '@/assets/js/utils'
import PageTitle from '@/components/scb/PageTitle.vue'
import Config from '@/assets/js/config'
import { useMultiProcRequest } from '@/assets/js/useMultiProcRequest'

const { results, fetchForProcs } = useMultiProcRequest()

const { proxy } = getCurrentInstance()
const router = useRouter()
const route = useRoute()

const gameInfo = ref({
  discipline: '马术',
  match: '',
  event: 'JUMPINDV',
  type: '热身',
  order: '',
})
let timer1 = null

const params = ref({})
const procName = ref([])
const runTime = ref(90)
const isRunning = ref(false)
const commonStore = useStore.useCommonStore()

onBeforeMount(() => {})
onMounted(() => {
  params.value = route.query
  procName.value = Config.procNameList[route.name]
  getData()
})

// 准备时间倒计时
const startTimer = () => {
  isRunning.value = true
  timer = setInterval(() => {
    runTime.value -= 1
    if (runTime.value == 0) {
      runTime.value = 90
    }
  }, 1000)
}

const getData = async () => {
  if (procName.value.length > 0) {
    fetchForProcs(procName.value, {
      procName: procName.value[0],
      keyValues: params.value,
    })
  }
}

watchEffect(() => {
  if (results.value?.recordset?.length) {
    gameInfo.value.event = results.value.recordset[0]?.EventCode
    gameInfo.value.match = results.value.recordset[0].EventName
  }
})

watch(
  () => commonStore.receivedActionMessages,
  (newVal, oldVal) => {
    if (newVal.length > 0) {
      const latestMsg = newVal[newVal.length - 1]
      console.log(latestMsg)
      if (isRunning.value) {
        clearInterval(timer1)
        isRunning1.value = false
      }
      runTime.value -= 1
      startTimer()
    }
  }
)
</script>

<style scoped lang="scss">
@import '@/assets/scss/scb/warnUp';
</style>
